import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'

// 约定大于配置，采用约定式路由：
/**读取 views 文件夹下所有的 pages.ts 的文件 */
const pages = import.meta.glob('../views/**/pages.ts', {
  eager: true, // 整个默认
  import: 'default', // 获取默认导出结果数据
})
console.log(" pages ", pages)


const components = import.meta.glob('../views/**/index.vue')

console.log(" components ", components)
const routes = Object.entries(pages).map(([path, meta]) => {
  const comPath = path.replace('pages.ts', 'index.vue')
  path = path.replace('../views','').replace('/pages.ts', '') || '/'
  const name = path.split('/').filter(Boolean).join('-') || 'index'

  console.log("path", path)
  console.log("name", name)
  console.log("comPath", comPath)
  return {
    path,
    name,
    component: components[comPath],
    meta,
  } as RouteRecordRaw
})

console.log("routes", routes)



const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
})

export default router
